package com.yough.api.framework.template;

import com.yough.api.framework.bean.ApiDocumentBean;
import com.yough.api.framework.constant.DocumentConstants;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description 生成HTML文件
 * @Author yough
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2021/1/8
 */
public class HtmlTemplate {

    /**
     * 生成html文件
     */
    public void createHtmlPage() throws Exception {
        //创建一个freemarker.template.Configuration实例，它是存储 FreeMarker 应用级设置的核心部分
        //指定版本号
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
        //设置模板目录
        cfg.setDirectoryForTemplateLoading(new File("F:\\Idea_Workspace\\Java-Senior-Projects\\Java-Practise-Projects\\api-framework\\src\\main\\resources\\templates"));
        //设置默认编码格式
        cfg.setDefaultEncoding("UTF-8");

        //生成主页内容
        // 构建数据模型
        ConcurrentHashMap<String, ApiDocumentBean> apiDocumentBeanMapVo = DocumentConstants.apiDocumentBeanMap;
        Map outVoMap = new HashMap();
        outVoMap.put("apiDocumentBeanMapVo",apiDocumentBeanMapVo);
        Template template = cfg.getTemplate("index.ftl");
        Writer out = new FileWriter(new File("F:\\Idea_Workspace\\Java-Senior-Projects\\Java-Practise-Projects\\api" +
                "-framework\\src\\main\\resources\\templates\\index.html"));
        template.process(outVoMap, out);
        //关闭
        out.flush();
        out.close();

        // 生成接口详情内容
//        template = cfg.getTemplate("api_detail_info.ftl");
//        // 构建数据模型
//        apiDocumentBeanMap = new HashMap<>();
//        apiDocumentBeanMap.put("apiDocumentBean", apiDocumentBean);
//        template.process(apiDocumentBeanMap, out);
//        out = new FileWriter(new File("F:\\Idea_Workspace\\Java-Senior-Projects\\Java-Practise-Projects\\api" +
//                "-framework\\src\\main\\resources\\templates\\api_detail_info.html"));
//        template.process(apiDocumentBeanMap, out);
//        //关闭
//        out.flush();
//        out.close();

    }
}
